package com.mxpio.erp.sales.controller;


import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.order.service.NoticeService;
import com.mxpio.erp.common.sales.entity.RejectBill;
import com.mxpio.erp.common.sales.service.RejectBillService;
import com.mxpio.erp.common.vo.OrderExcuteVo;
import com.mxpio.erp.sales.policy.impl.DefaultNoticePolicy;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "RejectBillController", description = "销售退货通知单接口")
@RestController
@RequestMapping("/erp/sales/rb/")
public class RejectBillController {

    @Autowired
    private RejectBillService service;

    @Autowired
    private NoticeService noticeService;

    @GetMapping("list")
    @Operation(summary = "退货通知单列表", description = "获取退货通知单列表（无分页）", method = "GET")
    public Result<List<RejectBill>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<RejectBill> rejectBills = service.list(RejectBill.class, criteria);
        return Result.OK(rejectBills);
    }

    @GetMapping("page")
    @Operation(summary = "退货通知单列表", description = "获取退货通知单列表（分页）", method = "GET")
    public Result<Page<RejectBill>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<RejectBill> rejectBills = service.listPage(RejectBill.class, page, criteria);
        return Result.OK(rejectBills);
    }

    @GetMapping("list/{noticeNo}")
    @Operation(summary = "根据noticeNo获取退货通知单", description = "根据noticeNo获取退货通知单", method = "GET")
    public Result<RejectBill> getById(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        RejectBill rejectBill = service.getById(RejectBill.class, noticeNo);
        return Result.OK(rejectBill);
    }

    @PostMapping("add")
    @Operation(summary = "新增退货通知单", description = "新增退货通知单", method = "POST")
    public Result<RejectBill> add(@RequestBody RejectBill rejectBill) {
        service.save(rejectBill);
        return Result.OK(rejectBill);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑退货通知单", description = "编辑退货通知单", method = "PUT")
    public Result<RejectBill> edit(@RequestBody RejectBill rejectBill) {
        service.update(rejectBill);
        return Result.OK(rejectBill);
    }

    @DeleteMapping("remove/{noticeNos}")
    @Operation(summary = "根据noticeNo删除退货通知单", description = "删除退货通知单", method = "DELETE")
    public Result<RejectBill> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "noticeNos", required = true) String noticeNos) {
        String[] noticeNo = noticeNos.split(",");
        for(String key : noticeNo){
            service.delete(RejectBill.class, key);
        }
        return Result.OK();
    }

    @RequestMapping("execute/{noticeNo}")
    @Operation(summary = "通知单执行", description = "通知单执行")
    public Result<Object> excute(@PathVariable(name = "noticeNo", required = true) String noticeNo,
                                 @RequestBody OrderExcuteVo orderExcuteVo) {
        RejectBill notice = service.getById(RejectBill.class, noticeNo);
        if (notice.getNoticeStatus().equals(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode())){
            return Result.error("单据状态已发生变化，已确认状态单据不允许重复确认！");
        }
        NoticeContext context = noticeService.execute(notice, new DefaultNoticePolicy(orderExcuteVo.getLines()));
        if(CollectionUtils.isEmpty(context.getErrors())) {
            return Result.OK();
        }else {
            return Result.error(context.getErrors().get(0));
        }
    }

    @PutMapping("reject/{noticeNo}")
    @Operation(summary = "拒绝通知单", description = "拒绝通知单", method = "PUT")
    public Result<? extends Notice> close(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
    	RejectBill rejectBill = service.getById(RejectBill.class,noticeNo);
    	return noticeService.updateNoticeStatus(rejectBill, NoticeStatusEnums.NoticeStatus.REJECT);
    }

    @GetMapping("listbynos/{noticeNos}")
    @Operation(summary = "根据noticeNos获取对应退货通知单及明细", description = "根据noticeNos获取退货通知单及明细", method = "GET")
    public Result<List<RejectBill>> getByIds(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
        return service.listByIds(noticeNos);
    }
}
